package com.liao.furns.web;
/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */

import com.liao.furns.pojo.Member;
import com.liao.furns.service.MemberService;
import com.liao.furns.service.impl.MemberServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    MemberService memberService = new MemberServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("RegisterServlet is called");
        String username = request.getParameter("username");
//        System.out.println("username = " + username);
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        if(!memberService.isExistUsername(username)){
//            System.out.println("用户名不存在，可以注册");
            Member member = new Member(null, username, password, email);
            if(memberService.registerMember(member)){
                request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request,response);
            }
        }else {
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
        }
    }
}
